package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.service.Gwat800Service;
import cn.e4j.modular.api.service.Gwat810Service;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.crm.entity.GwcwT;
import cn.e4j.modular.crm.mapper.GwaaTMapper;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单商品评价 控制类
 * Created by shixc on 2018/5/7.
 */
@RestController
@RequestMapping("api/wx/gwat800")
public class Gwat800Controller {
    private static Logger logger = LoggerFactory.getLogger(Gwat800Controller.class);

    @Autowired
    Gwat800Service gwat800Service;

    @Autowired
    Gwat810Service gwat810Service;

    @Autowired
    GwaaTMapper gwaaTMapper;
    /**
     * 获取业务人员 订单商品评价
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getGwcsDatas")
    public ResponseData getGwcsDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        int errcode = 200;
        String errmsg = "ok";
        List<Dto> list = new ArrayList<Dto>();
        SysUser user = wapper.getSysUser();
        Page<Object> page = wapper.getPage();
        try {
            UpdateWrapper<GwaaT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWAAENT",inDto.getString("ENT")).eq("GWAA001",user.getACCOUNT());
            GwaaT gwaaat = gwaaTMapper.selectOne(wrapper);
            inDto.put("GWAA001",user.getACCOUNT());
            //Dto dto = gwat800Service.getGwaaData(inDto);
            if (gwaaat.getGWAA007().equals("0")) {
                if (inDto.getString("status").equalsIgnoreCase("N")) {  //status:N未评价 Y:已评价
                    list = gwat800Service.getGwcs1(page,inDto);
                } else if (inDto.getString("status").equalsIgnoreCase("Y")) {
                    list = gwat800Service.getGwcs2(page,inDto);
                } else {
                    list = gwat800Service.getGwcs3(page,inDto);
                }
            } else if (gwaaat.getGWAA007().equals("1")) {
                if (inDto.getString("status").equalsIgnoreCase("N")) {
                    list = gwat800Service.getGwcs4(page,inDto);
                } else if (inDto.getString("status").equalsIgnoreCase("Y")) {
                    list = gwat800Service.getGwcs5(page,inDto);
                } else {
                    list = gwat800Service.getGwcs6(page,inDto);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            errcode = 500;
            errmsg = "服务器出小差了";
            return new ErrorResponseData(errcode,errmsg);
        }

        return new SuccessResponseData(list);
    }

    /**
     * 保存订单评价信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveData")
    public ResponseData saveData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        GwcwT gwcw_tPO = new GwcwT();
        List<GwcwT> gwcw_tPOs = new ArrayList<>();
        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
            String gwcw004 = inJson.getString("GWCW004");
            String[] gwcw004s = gwcw004.split(",");
            String gwcw005 = inJson.getString("GWCW005");
            String[] gwcw005s = gwcw005.split(",");
            gwcw_tPO = JSON.parseObject(JSON.toJSONString(inDto),GwcwT.class);
            gwcw_tPO.setGWCWENT(inJson.getString("GWCSENT"));
            gwcw_tPO.setGWCWDOCNO("SPPJ" + DateUtils.getDate("yyyyMMddHHmmssSSS"));
            LocalDate date = LocalDate.now();
            gwcw_tPO.setGWCW001("1");
            gwcw_tPO.setGWCW002(inJson.getString("GWCSDOCNO"));
            gwcw_tPO.setGWCW003(inJson.getBigDecimal("GWCSSEQ"));
            gwcw_tPO.setGWCW006(inJson.getString("GWCW006"));
            gwcw_tPO.setGWCW007(inJson.getString("GWCW007"));
            gwcw_tPO.setGWCW008(inJson.getString("GWCW008"));
            UpdateWrapper<GwaaT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWAAENT",inDto.getString("ENT"));
            wrapper.eq("GWAA001",currentUser.getACCOUNT());
            GwaaT gwaaT = gwaaTMapper.selectOne(wrapper);
            gwcw_tPO.setGWCW009(gwaaT.getGWAA002());
            gwcw_tPO.setGWCW010(gwaaT.getGWAA004());
            gwcw_tPO.setGWCW011(gwaaT.getGWAA007());
            gwcw_tPO.setGWCW012(gwaaT.getGWAA003());
            gwcw_tPO.setGWCW020(inJson.getString("GWCS001"));
            gwcw_tPO.setGWCWORIU(currentUser.getID());
            for(int i = 0; i < gwcw004s.length; i ++){
                GwcwT gwcwT = parseObj(gwcw_tPO, GwcwT.class);
                gwcwT.setGWCWDOCDATE(date);
                gwcwT.setGWCWSEQ(BigDecimal.valueOf(i+1));
                gwcwT.setGWCW004(gwcw004s[i]);
                gwcwT.setGWCW005(gwcw005s[i]);
                gwcw_tPOs.add(gwcwT);
            }
            gwat800Service.saveData(gwcw_tPOs);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(500,"服务器出小差了");
        }
        return new SuccessResponseData();
    }

    /**
     * 获取业务人员 订单运输评价
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMmrj")
    public ResponseData getMmrj(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = Dtos.newDto();
        List<Dto> list = new ArrayList<Dto>();
        try {
            inDto.put("MMRJENT", inDto.getString("ENT"));
            list = gwat810Service.getMmrj(inDto);
            outDto.put("data", list);
            outDto.setAppCode(200);
            outDto.setAppMsg("OK");
        } catch (Exception e) {
            outDto.setAppCode(500);
            outDto.setAppMsg("服务器出小差了");
        }
        return new SuccessResponseData(outDto);
    }

    public static <T> T parseObj(Object fromObj,Class<T> clazz){
        T t = JSON.parseObject(JSON.toJSONString(fromObj), clazz);
        return t;
    }

}

